package com.tianbing.adlottery.controller.hordeadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;

import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.IDeltaDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.entity.DeltaRelation;
import com.tianbing.adlottery.controller.AdLotterySimpleFormController;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyHordeRecommendStoreProductController extends AdLotterySimpleFormController {
	static Logger logger = Logger.getLogger(EditMyHordeRecommendStoreProductController.class.getName());

	private IContentDao contentDao;

	private ISiteDao siteDao;

	private IDeltaDao deltaDao;

	private ISiteBusiness siteBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		long s2scsid = ServletRequestUtils.getLongParameter(request, "s2scsid", -1);
		DeltaRelation s2sc = deltaDao.findDeltaRelationBy(s2scsid);
		fb.setDeltaRelation(s2sc);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;

		DeltaRelation s2sc = form.getDeltaRelation();

		siteBusiness.updateHordeRecommendStoreProduct(auth.getUserSysId(), s2sc);

		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMyHordeRecommendStoreCouponController.logger = logger;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setDeltaDao(IDeltaDao deltaDao) {
		this.deltaDao = deltaDao;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

}
